package com.example.administrator.myokhttpmvp.main.login.presenter;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.example.administrator.myokhttpmvp.main.login.loginActivity;
import com.example.administrator.myokhttpmvp.main.login.model.ILoginModel;
import com.example.administrator.myokhttpmvp.main.login.model.loginModel;
import com.example.administrator.myokhttpmvp.main.login.view.ILoginView;
import com.example.administrator.myokhttpmvp.net.NetCallBackImp;
import com.example.administrator.myokhttpmvp.net.ServerApi;
import com.example.administrator.myokhttpmvp.net.UserNet;
import com.example.administrator.myokhttpmvp.utils.PrefManager;
import com.example.administrator.myokhttpmvp.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by kaede on 2015/5/18.
 */
public class LoginPresenterCompl implements ILoginPresenter {
	ILoginView iLoginView;
	ILoginModel user;

	public LoginPresenterCompl(ILoginView iLoginView) {
		this.iLoginView = iLoginView;
	}

	@Override
	public void clear() {
		iLoginView.onClearText();
	}

	@Override
	public void doLogin(String name, String passwd) {
        app_login(name,passwd);
	}

    private void app_login(String name, String passwd) {
        try {
            UserNet.Instance().app_login(name, passwd, new NetCallBackImp() {

                @Override
                public void pareOk(JSONObject data) {
                    super.pareOk(data);
                    Log.d("======sss", data.toString());

                    try {

                        if (data.has("token")) {
                            PrefManager.getInstance().setString(ServerApi.SP_TOKEN, data.getString("token"));
                            Log.d("=====token", data.getString("token"));
                        }

                        iLoginView.onLoginResultSucess(true, 0,"");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void paresOther(String code, String message) {
                    super.paresOther(code, message);
                    iLoginView.onLoginResultOther(false, 2,"");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	@Override
	public void setProgressBarVisiblity(int visiblity){
		iLoginView.onSetProgressBarVisibility(visiblity);
	}

}
